package org.yak.common.digest;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * 加密算法，配合前台
 * @author xuqiang
 *
 */
public class CryptUtils {

	/**
	 * 加密算法
	 */
	private static final String BLOWFISH = "Blowfish";

	/**
	 * 密钥
	 */
	private static final String SECRET_KEY = "az@#09?%";

	/**
	 * 字符编码
	 */
	private static final String CHARSET = "UTF-8";

	/**
	 * 加密
	 *
	 * @param value
	 * @return
	 */
	public static String encrypt(String value) {
		if (value == null) {
			return value;
		}
		try {
			SecretKey deskey = new SecretKeySpec(SECRET_KEY.getBytes(),
					BLOWFISH);
			Cipher cipher = Cipher.getInstance(BLOWFISH);
			cipher.init(Cipher.ENCRYPT_MODE, deskey);
			byte[] data = cipher.doFinal(value.getBytes(CHARSET));
			data = Base64.encodeBase64(data);
			return URLEncoder.encode(new String(data, CHARSET), CHARSET);
		} catch (Exception e) {
			throw new RuntimeException("encrypt exception!", e);
		}
	}

	/**
	 * 解密
	 *
	 * @param value
	 * @return
	 */
	public static String decrypt(String value) {
		if (value == null) {
			return value;
		}
		try {
			value = URLDecoder.decode(value, CHARSET);
			SecretKey deskey = new SecretKeySpec(SECRET_KEY.getBytes(),
					BLOWFISH);
			Cipher cipher = Cipher.getInstance(BLOWFISH);
			cipher.init(Cipher.DECRYPT_MODE, deskey);
			byte[] data = Base64.decodeBase64(value.getBytes(CHARSET));
			data = cipher.doFinal(data);
			return new String(data, CHARSET);
		} catch (Exception e) {
			throw new RuntimeException("decrypt exception!", e);
		}
	}

	public static String md5Encode(String rawpassword) {
		byte[] encoded = Base64.encodeBase64(DigestUtils.md5(rawpassword));
		return new String(encoded);
	}

	public static String Base64Encode(String str){
		byte[] encoded = Base64.encodeBase64(str.getBytes());
		return new String(encoded);

	}

	public static String Base64DEncode(String str){
		byte[] dencode = Base64.decodeBase64(str.getBytes());
		return new String(dencode);
	}

	/**
	 * 生成短信验证随机码
	 * 去除数字和字符有混淆的字符
	 * @return
	 */
	public static String genRandomCode(int len) {
		char[] validateChars = { 'A','C', 'D', 'E', 'F','H',
				'J', 'K','M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
				'V', 'W', 'X', 'Y','a', 'b', 'c', 'd', 'e', 'f', 'g',
				'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's',
				't', 'u', 'v', 'w', 'x', 'y', '3', '4',
				'5', '6', '7'};
		StringBuilder builder = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			int randomIndex = random.nextInt(validateChars.length);
			builder.append(validateChars[randomIndex]);
		}
		return builder.toString();
	}

	/**
	 * 字符转换字符串
	 * @param chars
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String charsToString(String chars)
		    throws UnsupportedEncodingException
	  {
	    String[] strs = chars.split(",");
	    char[] bts = new char[strs.length];
	    for (int i = 0; i < strs.length; i++) {
	      bts[i] = ((char)Integer.parseInt(strs[i]));
	    }
	    return new String(bts);
	  }

}
